package automate.learn;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import automate.learn.UserUtilities;
import automate.learn.GlobalVariables;

public class LoginTest {

	WebDriver driver = null;
    
	@Before
	public void WebElement() throws Exception {
		driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		driver.get(GlobalVariables.loginLink);
		Boolean isElementPresent = driver.findElements(By.xpath("//a[@id = 'ctl00_ContentPlaceHolder1_Login1_btnLogin']"))
		        .size() != 0;

		if (isElementPresent == true) {
			System.out.println("Continue test");
		} else {
			Assert.fail("Test Failed");
		}
	}
	
	@Test
	public void loginTest() throws Exception {
		UserUtilities userUtilities = new UserUtilities();
		User user;
		ArrayList<User> users = userUtilities.getUsersFromXMLFile("Users.xml");

		for (int i = 0; i <= users.size(); i++) {
			user = users.get(i);
			Login(user);
		}
	}


	public void Login(User user){
		driver.findElement(By.xpath("//input[@name = 'ctl00$ContentPlaceHolder1$Login1$txtLogin']"))
				.sendKeys(user.getUserName());
		System.out.println(user.getUserName());
		driver.findElement(By.xpath("//input[@name = 'ctl00$ContentPlaceHolder1$Login1$txtPassword']"))
		        .sendKeys(user.getPassword());
		System.out.println(user.getPassword());
		driver.findElement(By.xpath("//a[@id = 'ctl00_ContentPlaceHolder1_Login1_btnLogin']"))
		        .click();
		System.out.println(driver.getCurrentUrl());
		if (driver.getCurrentUrl().equals(GlobalVariables.loginLink)) {
			Assert.assertTrue("Test Failed", true);
			System.out.println("Test Failed!");
			Boolean isFaildMessagePresent = driver.findElements(By.xpath("//span[@class = 'ErrorLabel']"))
			        .size() != 0;

			if (isFaildMessagePresent == true) {
				System.out.println("Failed message present. Test Passed!");
			} else {
				System.out.println("Failed message is not present. Test Failed!");
			}

			driver.findElement(
			        By.xpath("//input[@name = 'ctl00$ContentPlaceHolder1$Login1$txtLogin']"))
			        .clear();
		} else {
			WebDriverWait wait = new WebDriverWait(driver, 300);
			wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//span[@class = 'rmText rmExpandDown']")));
			Boolean isPresentElement = driver.findElement(By.xpath("//a[@class = 'MenuCurrent']"))
			        .isDisplayed();
			Assert.assertTrue("Test Passed", isPresentElement);
			System.out.println("Test Passed!");
			driver.findElement(By.xpath("//a[@id = 'ctl00_lnkLogout']"))
			        .click();
			WebDriverWait waitLogout = new WebDriverWait(driver, 300);
			waitLogout.until(ExpectedConditions.titleIs("360 - Admin"));
		}
	}	


    @After
    public void setup() throws Exception {
    	driver.quit();
    }
}
